package com.blrs.service.activity.impl;

import java.util.List;
import java.util.UUID;

import com.blrs.common.core.domain.entity.SysUser;
import com.blrs.common.utils.SecurityUtils;
import com.blrs.mapper.ActivityReceptionMapper;
import com.blrs.service.activity.IActivityRegistrationService;
import com.blrs.system.mapper.SysDeptMapper;
import com.blrs.system.mapper.SysUserMapper;
import com.blrs.system.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.blrs.mapper.ActivityRegistrationMapper;
import com.blrs.domain.ActivityRegistration;
import org.springframework.transaction.annotation.Transactional;

/**
 * 活动培训报名信息Service业务层处理
 * 
 * @author yang
 * @date 2023-11-24
 */
@Service
public class ActivityRegistrationServiceImpl implements IActivityRegistrationService
{
    @Autowired
    private ActivityRegistrationMapper activityRegistrationMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private ActivityReceptionMapper activityReceptionMapper;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询活动培训报名信息
     * 
     * @param applicationId 活动培训报名信息主键
     * @return 活动培训报名信息
     */
    @Override
    public ActivityRegistration selectActivityRegistrationByApplicationId(String applicationId)
    {
        return activityRegistrationMapper.selectActivityRegistrationByApplicationId(applicationId);
    }

    /**
     * 查询活动培训报名信息列表
     * 
     * @param activityRegistration 活动培训报名信息
     * @return 活动培训报名信息
     */
    @Override
    public List<ActivityRegistration> selectActivityRegistrationList(ActivityRegistration activityRegistration)
    {

        return activityRegistrationMapper.selectActivityRegistrationList(activityRegistration);
    }

    /**
     * 新增活动培训报名信息
     * 
     * @param activityRegistration 活动培训报名信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertActivityRegistration(ActivityRegistration activityRegistration)
    {
        activityRegistration.setDeptName(SecurityUtils.getOrgName());
        activityRegistration.setDeptId(SecurityUtils.getOrgId());
        activityRegistration.setApplicationId(UUID.randomUUID().toString().replaceAll("-",""));
         //判断当前这个员工的企业是否对于这个活动已经报名
        if (activityReceptionMapper.selectIsPart(activityRegistration.getActivityId(),SecurityUtils.getOrgId())==0){
          //说明没有报名，直接报名即可
            activityReceptionMapper.updateIsPart(activityRegistration.getActivityId(),SecurityUtils.getOrgId());
        }
        return activityRegistrationMapper.insertActivityRegistration(activityRegistration);
    }

    /**
     * 修改活动培训报名信息
     * 
     * @param activityRegistration 活动培训报名信息
     * @return 结果
     */
    @Override
    public int updateActivityRegistration(ActivityRegistration activityRegistration)
    {
        return activityRegistrationMapper.updateActivityRegistration(activityRegistration);
    }

    /**
     * 批量删除活动培训报名信息
     *
     * @param applicationIds 需要删除的活动培训报名信息主键
     * @param activityId
     * @return 结果
     */
    @Override
    public int deleteActivityRegistrationByApplicationIds(String[] applicationIds, String activityId)
    {
        int i = activityRegistrationMapper.deleteActivityRegistrationByApplicationIds(applicationIds);
        //判断该企业报名人员中还是否存在数据
        if (activityRegistrationMapper.selectApplicationCount(activityId,SecurityUtils.getOrgId())==0){
            //说明没有报名人员信息了，那么就把报名企业是否报名取消
          activityReceptionMapper.updateNoPart(activityId,SecurityUtils.getOrgId());
        }
   return i;
    }

    /**
     * 删除活动培训报名信息信息
     * 
     * @param applicationId 活动培训报名信息主键
     * @return 结果
     */
    @Override
    public int deleteActivityRegistrationByApplicationId(String applicationId)
    {
        return activityRegistrationMapper.deleteActivityRegistrationByApplicationId(applicationId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertActivityApp(String activityId) {
        ActivityRegistration activityRegistration = new ActivityRegistration();
        activityRegistration.setApplicationId(UUID.randomUUID().toString().replaceAll("-",""));
        activityRegistration.setActivityId(activityId);
        activityRegistration.setDeptId(SecurityUtils.getOrgId());
        activityRegistration.setPersonnelId(SecurityUtils.getUserId());
        activityRegistration.setPersonnelName(SecurityUtils.getUsername());
        activityRegistration.setDeptName(SecurityUtils.getOrgName());
        //查询当前人员的职位和联系方式信息
        SysUser sysUser = sysUserMapper.selectUserById(SecurityUtils.getUserId());
        activityRegistration.setPersonnelDuties(sysUser.getPosition());
        activityRegistration.setPersonnelContact(sysUser.getPhonenumber());
        //改变接收企业报名情况
        activityReceptionMapper.updateIsPart(activityId,SecurityUtils.getOrgId());
        //添加报名情况
    return     activityRegistrationMapper.insertActivityRegistration(activityRegistration);
    }

    @Override
    public List<ActivityRegistration> selectActivityRegistrationByActivityId(ActivityRegistration activityRegistration) {
        return activityRegistrationMapper.selectActivityRegistrationByActivityId(activityRegistration);
    }

    @Override
    public List<ActivityRegistration> selectActivityRegistrationListExport(ActivityRegistration activityRegistration) {
        return activityRegistrationMapper.selectActivityRegistrationListExport(activityRegistration);
    }
}
